package com.web;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.domain.Carts;
import com.domain.User;
import com.service.CartService;
import com.service.Impl.CartServiceImpl;
import com.service.Impl.UsersServiceImpl;
import com.service.UsersService;

@WebServlet("/vipServlet")
public class vipServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取域中数据
        HttpSession session = request.getSession();
        //获取登录用户名
        String username = (String) session.getAttribute("username");
        if(username == null) {
            //没有登陆跳转登录页面
            request.getRequestDispatcher("/page/login1.jsp").forward(request, response);
            return;
        }
        //获取登录用户名信息
        UsersService usersService = new UsersServiceImpl();
        User user = usersService.findByName(username);
        int uid = user.getId();//得到用户ID值

        System.out.println(user);
        //根据ID查询地址信息及电话号码
        CartService cartService = new CartServiceImpl();
        List<Carts> carts = cartService.findIdName(uid);
        System.out.println("购物车" + carts);

        session.setAttribute("carts", carts);
        //重定向到购物车
        response.sendRedirect("/woniusales/page/vipPage.jsp");

    }
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}
